package hng.mapper;

import hng.model.ProductCommentDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import hng.vo.ProductCommentVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品评论表 Mapper 接口
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
public interface ProductCommentMapper extends BaseMapper<ProductCommentDO> {

    /**
     * 用户查询某个商品的评论列表
     */
    @Select("select u.username, u.avatar, c.* " +
            "from product_comment c " +
            "left join user u " +
            "on c.user_id = u.id " +
            "where c.product_id = #{productId} " +
            "order by c.create_time desc ")
    List<ProductCommentVO> getListByProductId(@Param("productId") Integer productId);

    /**
     * 商家查询某个商品的评论列表
     */
    @Select("<script>" +
            "select u.username, u.avatar, c.* " +
            "from product_comment c " +
            "left join user u on c.user_id = u.id " +
            "left join product p on c.product_id = p.id " +
            "where p.store_id = #{storeId} " +
            "<if test='keyword != null and keyword.trim() != \"\"'>" +
            "  and (u.username like concat('%', #{keyword}, '%') " +
            "  or c.content like concat('%', #{keyword}, '%'))" +
            "</if>" +
            "order by c.create_time desc" +
            "</script>")
    List<ProductCommentVO> getListByStore(@Param("storeId") Integer storeId, @Param("keyword") String keyword);

    /**
     * 管理员查询商品的评论列表
     */
    @Select("<script>" +
            "select u.username, u.avatar, c.* " +
            "from product_comment c " +
            "left join user u on c.user_id = u.id " +
            "left join product p on c.product_id = p.id " +
            "where 1 = 1 " +
            "<if test='keyword != null and keyword.trim() != \"\"'>" +
            "  and (u.username like concat('%', #{keyword}, '%') " +
            "  or c.content like concat('%', #{keyword}, '%'))" +
            "</if>" +
            "order by c.create_time desc" +
            "</script>")
    List<ProductCommentVO> getListByAdmin(@Param("keyword") String keyword);

    /**
     * 商家查询评论总数
     */
    @Select("SELECT COUNT(p.id) " +
            "FROM product_comment c " +
            "LEFT JOIN product p ON c.product_id = p.id " +
            "WHERE p.store_id = #{storeId}")
    int countCommentsByStoreId(@Param("storeId") int storeId);
}
